﻿Imports System.Text.RegularExpressions
Imports System.Configuration
Public Class frmMain
    Public username As String = ""
    Public loai As String = 0
    Public Shared st As String = "-1"
    Private Sub HienTimKiem()
        gbTimKiem.Show()
    End Sub
    Private Sub AnTimKiem()
        gbTimKiem.Hide()
    End Sub
    Private Sub frmLoad()
        gbDangNhap.Hide()
        lbTrangThai.Text = "Bạn chưa đăng nhập"
      
        mLogout.Enabled = False
        mQuanLyTaiKhoan.Enabled = False
        mQuanLyCapCao.Enabled = False
        mQuanLy.Enabled = False
        mXuat.Enabled = False


        mHDSDAdmin.Enabled = False
        mHDSDNhanVien.Enabled = False

        mHuongDanSuDung.Enabled = False '''''''''
        mTimKiem.Enabled = False ''''''''''

        dgvDanhSachNha.DataSource = NhaThueBUS.GetNha_KhachHangBUS()
        dgvDanhSachNha.Visible = False '''''''
        AnTimKiem()

        cbLoaiNhaTimKiem.DataSource = LoaiNhaThueBUS.GetAllLoaiNhaThueBUS()
        cbLoaiNhaTimKiem.DisplayMember = "TenLoaiNha"
        cbLoaiNhaTimKiem.ValueMember = "MaLoaiNha"

        cbQuanTimKiem.DataSource = QuanBUS.GetAllQuanBUS()
        cbQuanTimKiem.DisplayMember = "TenQuan"
        cbQuanTimKiem.ValueMember = "MaQuan"
    End Sub
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try


            Dim state As String = AppSettingsWriter.GetValue("CauHinh")

            If state = "1" Then
                frmLoad()
            Else
                If state = "0" Then
                    Dim frm As frmCauHinh = New frmCauHinh()
                    frm.ShowDialog()
                    If st = "1" Then
                        frmLoad()
                    End If
                End If

            End If
        Catch ex As Exception
            MessageBox.Show("Kết nối bị lỗi bạn vui lòng kiểm tra lại cơ sở dữ liệu và server")
        End Try
    End Sub
    Private Sub mCongTyMoiGioi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mCongTyMoiGioi.Click
        AnTimKiem()
        Dim frm As frmCongTyMoiGioi = New frmCongTyMoiGioi()
        frm.username = username
        frm.ShowDialog()

    End Sub

    Private Sub mNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mNha.Click
        AnTimKiem()
        Dim frm As frmNha = New frmNha()
        frm.username = username
        frm.ShowDialog()
    End Sub

    Private Sub mBaoCao_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mBaoCao.Click
        AnTimKiem()
        Dim frm As frmXuatBaoCao = New frmXuatBaoCao()

        frm.ShowDialog()
    End Sub

    Private Sub mXuatFileExcel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mXuatFileExcel.Click
        AnTimKiem()

        Dim frm As frmChuyenSangFileExcel = New frmChuyenSangFileExcel()
        frm.username = username
        frm.ShowDialog()
    End Sub

    Private Sub mLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mLogin.Click
        AnTimKiem()
        gbDangNhap.Show()

        'Dim frm As frmDangNhap = New frmDangNhap()
        'frm.ShowDialog()
    End Sub

    Private Sub mQuanLyTaiKhoan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mQuanLyTaiKhoan.Click
        AnTimKiem()
        Dim frm As frmUser = New frmUser()

        frm.ShowDialog()
    End Sub

    Private Sub mDuLieuTamXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mDuLieuTamXoa.Click
        AnTimKiem()
        Dim frm As frmDuLieuTamXoa = New frmDuLieuTamXoa()

        frm.ShowDialog()
    End Sub

    Private Sub mCapNhatLoaiNha_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mCapNhatLoaiNha.Click
        AnTimKiem()
        Dim frm As frmLoaiNha = New frmLoaiNha()

        frm.ShowDialog()
    End Sub

    Private Sub mCapNhatTinhTrang_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mCapNhatTinhTrang.Click
        AnTimKiem()
        Dim frm As frmTinhTrang = New frmTinhTrang()

        frm.ShowDialog()
    End Sub

    Private Sub mCapNhatBangThamSo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mCapNhatBangThamSo.Click
        AnTimKiem()
        Dim frm As frmCapNhatThamSo = New frmCapNhatThamSo()
        frm.username = username
        frm.ShowDialog()
    End Sub

    Private Sub mCapNhatGiaTriDinhSan_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mCapNhatGiaTriDinhSan.Click
        AnTimKiem()
        Dim frm As frmCapNhatGiaTriDinhSan = New frmCapNhatGiaTriDinhSan()
        frm.username = username
        frm.ShowDialog()
    End Sub

    Private Sub mCapNhatPhanQuyen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mCapNhatPhanQuyen.Click
        AnTimKiem()
        Dim frm As frmUser = New frmUser()

        frm.ShowDialog()
    End Sub

    Private Sub CậpNhậtQuậnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CậpNhậtQuậnToolStripMenuItem.Click
        AnTimKiem()
        Dim frm As frmQuan = New frmQuan()
        frm.username = username
        frm.ShowDialog()

    End Sub



    Private Sub mTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mTimKiem.Click
        HienTimKiem()

    End Sub



    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        Try

            Dim maloainha As Integer = Integer.Parse(cbLoaiNhaTimKiem.SelectedValue.ToString())
            Dim tenquan As String = cbQuanTimKiem.Text.ToString()
            Dim loai As Integer = cbTimKiem.SelectedIndex
            dgvDanhSachNha.DataSource = NhaThueBUS.TimKiemNha_KhachHangBUS(maloainha, tenquan, loai)
        Catch ex As Exception
            MessageBox.Show("Tìm kiếm bị lỗi")
        End Try
    End Sub

    Private Sub btnLoadLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadLai.Click
        dgvDanhSachNha.DataSource = NhaThueBUS.GetNha_KhachHangBUS()
    End Sub


    Private Sub mDuLieuSaoLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mDuLieuSaoLuu.Click
        '  MessageBox.Show("Để quá trình sao lưu liệu đảm bảo thành công, bạn không nên lưu nó ở Desktop")

        Dim SaveFileDialog1 As New SaveFileDialog
        SaveFileDialog1.Filter = "Backup Files (*.bak)|*.bak"
        SaveFileDialog1.ShowDialog()
        Dim path As String = SaveFileDialog1.FileName

        SqlDataProvider.BacKup(path)


            
    End Sub

    Private Sub mDuLieuPhucHoi_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mDuLieuPhucHoi.Click
        MessageBox.Show("Để quá trình phục hồi dữ liệu đảm bảo thành công, tốt nhất bạn nên tắt SqlServer")
        Dim SaveFileDialog1 As New SaveFileDialog
        SaveFileDialog1.Filter = "Backup Files (*.bak)|*.bak"
        SaveFileDialog1.ShowDialog()
        Dim path As String = SaveFileDialog1.FileName

        SqlDataProvider.Restore(path)
    End Sub

    Private Sub btnOk_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOk.Click
        Dim ten As String = txtUser.Text
        Dim pass As String = txtPass.Text

        Dim kq As Integer = ThanhVienBUS.KiemTraDangNhapBUS(ten, pass)
        If (kq <> 0) Then
            loai = kq
            username = ten
            lbTrangThai.Text = "Xin chào  " & username
            gbDangNhap.Hide()
            mHuongDanSuDung.Enabled = True  '''''''''
            mTimKiem.Enabled = True  ''''''''''
            mLogin.Enabled = False
            mLogout.Enabled = True
            dgvDanhSachNha.Visible = True '''''''
            If (loai = 3) Then
                mQuanLyTaiKhoan.Enabled = True
                mQuanLyCapCao.Enabled = True
                mHDSDAdmin.Enabled = True
            Else
                If (loai = 2) Then
                    mQuanLy.Enabled = True
                    mXuat.Enabled = True
                    mHDSDNhanVien.Enabled = True
                End If
               

            End If
        Else
            MessageBox.Show("Username hoặc pass không đúng")
        End If



    End Sub

    Private Sub mLogout_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mLogout.Click
        mLogin.Enabled = True
        mLogout.Enabled = False
        mQuanLyTaiKhoan.Enabled = False

        mQuanLyCapCao.Enabled = False
        mQuanLy.Enabled = False
        mXuat.Enabled = False

        mHDSDAdmin.Enabled = False
        mHDSDNhanVien.Enabled = False

        dgvDanhSachNha.Visible = False '''''''

        mHuongDanSuDung.Enabled = False '''''''''
        mTimKiem.Enabled = False ''''''''''

        txtPass.Text = ""
        txtUser.Text = ""
        lbTrangThai.Text = "Bạn chưa đăng nhập"
        loai = 0
        username = ""
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
        gbDangNhap.Hide()
    End Sub

   
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        dgvDanhSachNha.DataSource = NhaThueBUS.GetNha_KhachHangBUS()
    End Sub

  

    Private Sub TìmKiếmToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mHDSDTimKiem.Click
        Process.Start("Tim Kiem.chm")
        ' Process.Start("..\..\Tim Kiem.chm")
    End Sub

    Private Sub NhânViênToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mHDSDNhanVien.Click
        Process.Start("NhanVien.chm")
        ' Process.Start("..\..\NhanVien.chm")
    End Sub

    Private Sub AdminToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mHDSDAdmin.Click
        Process.Start("Admin.chm")
        'Process.Start("..\..\Admin.chm")
    End Sub
End Class